package com.just.springbootnosql.controller;

import com.just.springbootnosql.dao.PersonRepository;
import com.just.springbootnosql.domain.Location;
import com.just.springbootnosql.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonRepository personRepository;
    @PostMapping("/save")
    public Person save(@RequestBody Person person){
        Collection<Location> locations=new LinkedHashSet<>();
        Location location1=new Location("南京","2016");
        Location location2=new Location("常州","2016");
        Location location3=new Location("上海","2017");
        Location location4=new Location("上海","2018");
        locations.add(location1);
        locations.add(location2);
        locations.add(location3);
        locations.add(location4);
        person.setLocations(locations);
        return personRepository.save(person);
    }
    @GetMapping("/findByName")
    public Person findByName(String name){
        return personRepository.findByName(name);
    }
    @GetMapping("/findByAge")
    public List<Person> findByAge(Integer age){
        return personRepository.withQueryByAge(age);
    }
}
